﻿using System.Net;
using System.Text;
using EDC.DNC.MSAD.NB.HttpClient;
using SuperSocket.Channel;
using SuperSocket.Client;
using SuperSocket.Client.Proxy;
using SuperSocket.ProtoBase;


var connectors = new List<IConnector>();

while (true)
{
    for (int i = 2; i < 255; i++)
    {
        try
        {
            
            //121.229.41.156:5858
            IConnector proxyConnector1 = new Socks5Connector(new DnsEndPoint("36.104.146.120", 20002, System.Net.Sockets.AddressFamily.InterNetwork),"sss209","eee333",null);

            Console.WriteLine("正在连接...");

            var connectResult1 = await proxyConnector1.ConnectAsync(new IPEndPoint(IPAddress.Parse("103.102.202.75"), 80));

            Console.WriteLine($"连接状态：{connectResult1.Result}");

            //connectors.Add(proxyConnector1);
//223.240.92.244:5858/y1b51675864620/665577
            //continue;
            var address = Dns.GetHostAddresses("www.3322.org")[0];

            var http = new HttpCode(new HttpProxyItem
            {
                Username = "sss209",
                Password = "eee333",
                Type = SocketProxyType.Socket5,
                RemoteEndPoint =
                    new DnsEndPoint($"36.104.146.120", 20002, System.Net.Sockets.AddressFamily.InterNetwork),
            });

            var response = await http.SendAsync(new HttpRequestItem(
                "http://103.102.202.75/rest/n/system/realtime/startup?is_first_cold_start=true&c=a&did=AEE1EB2A-DACC-4143-A590-425760888234&kpn=KUAISHOU&grant_browse_type=INITIALIZATION&apiInvokeTiming=COLD_START&cold=true&keyconfig_state=1&sw=828&is_background=0&kpf=IPHONE&did_gt=1644902549855&sys=ios14.8&sh=1792&kcv=5&bottomPageType=2&browseType=4&net=_5&darkMode=false&ver=9.9&mod=iPhone11%2C8&isp=&cold_launch_time_ms=1644902549804&vague=0&appver=9.9.20.6440")
            {
                Method = HttpCode.HttpMethod.Post,
                Content =
                    "client_key=56c3713c&country_code=cn&cs=false&global_id=&kuaishou.api_st=&language=zh-Hans-CN%3Bq%3D1&lastQuitTab=1&launchMode=1&needPersonalizedTab=1&power_mode=0&sessionId=5A02E865-EEB7-4019-A072-EF9D3814F48B&sig=56c9215d02f29ceb8e1501e8acb4a44f&thermal=10000&token="
            });

            // using (var fs = new FileStream("proxy.txt", FileMode.Append))
            // {
            //     using (var s = new StreamWriter(fs, Encoding.UTF8))
            //     {
            //         await s.WriteLineAsync(string.Join(",", address, 4040, "wujun", "wujun520."));
            //     }
            // }


            var content = response.Body.GetString(Encoding.UTF8);

            Console.WriteLine(content);
            continue;

            IConnector proxyConnector =
                new Socks5Connector(
                    new DnsEndPoint($"104.143.159.{i}", 4040, System.Net.Sockets.AddressFamily.InterNetwork), "wujun",
                    "wujun520.", null);

            /*
            IConnector proxyConnector = new SocketConnector(new SslStreamConnector(new SecurityOptions
            {
                TargetHost = address.ToString(),
                EnabledSslProtocols = SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13,
                LocalCertificateSelectionCallback = delegate(object sender, string host,
                    X509CertificateCollection certificates, X509Certificate? certificate,
                    string[] issuers)
                {
                    return null;
                },
                RemoteCertificateValidationCallback = (sender, certificate, chain, errors) =>
                {
                    return true;
                }
            }));
            */


            Console.WriteLine("正在连接...");

            var connectResult = await proxyConnector.ConnectAsync(new IPEndPoint(address, 80));

            Console.WriteLine($"连接状态：{connectResult.Result}");

            var builder = new StringBuilder();

            //http://www.3322.org/dyndns/getip

            //builder.Append("POST /api/partner/live/startPlay HTTP/1.1").Append("\r\n");
            builder.Append("Host: www.3322.org").Append("\r\n");
            //builder.Append("Content-Type: application/x-www-form-urlencoded").Append("\r\n");
            //builder.Append("Cookie: appver=4.8.0.309232202; c=appstore; countryCode=CN; did=5B86490E-7D75-4AAD-AE54-AFDC789232B4; kpf=IPHONE; kpn=ks698057946357666011; language=zh-Hans; mcc=; ").Append("\r\n");
            builder.Append("Connection: keep-alive").Append("\r\n");
            //builder.Append("Accept: application/json").Append("\r\n");
            builder.Append("User-Agent: unknown").Append("\r\n");
            builder.Append("Accept-Language: zh-Hans;q=1, en;q=0.9").Append("\r\n");
            //builder.Append("Content-Length: 120").Append("\r\n");
            builder.Append("Content-Length: 0").Append("\r\n");
            //builder.Append("\r\n");

            //builder.Append("author=44510974&amp;clientId=23&amp;client_key=p7wguzx8&amp;liveStreamId=9298575055&amp;platform=IPHONE&amp;product=ALLIN_GAME_SDK&amp;source=18");

            var data = builder.ToString();

            var channel = connectResult.CreateChannel(new HttpPipelineFilter(), new ChannelOptions());
            channel.Start();

            var packageStream = channel.GetPackageStream();

            await channel.SendAsync(Encoding.UTF8.GetBytes(data));

            var packageInfo = await packageStream.ReceiveAsync();

            Console.WriteLine(packageInfo.Body);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

Console.ReadKey();